from threading import Thread
g_num = 0

"""
如果多个线程同时对同一个全局变量操作，会出现资源竞争问题，从而数据结果会不正确

---线程创建之前g_num is 0---
----in work1, g_num is 1115305---
----in work2, g_num is 1277614---

"""
def work1():
    global g_num
    for i in range(1000000):
        g_num += 1
    print("----in work1, g_num is %d---"%g_num)

def work2():
    global g_num
    for i in range(1000000):
        g_num += 1
    print("----in work2, g_num is %d---"%g_num)

print("---线程创建之前g_num is %d---"%g_num)

t1 = Thread(target=work1)
t1.start()

t2 = Thread(target=work2)
t2.start()
